﻿using DynamicRequestWebservice.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace DynamicRequestWebservice
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //固定参数(配置文件)
                string url = System.Configuration.ConfigurationSettings.AppSettings["url"];
                string className = System.Configuration.ConfigurationSettings.AppSettings["className"];
                string methodName = System.Configuration.ConfigurationSettings.AppSettings["methodName"];

                //动态参数
                string _username = "30451";
                string _password = "665897";
                string _system = "医疗质控平台";
                string _systemidentity = "20D78E42-DCB2-45C0-B062-2B8065C8671B";

                string xmlString = String.Empty;
                object responseObject = WebServiceHelper.InvokeWebService(url
                    , className
                    , methodName
                    , new object[] { _username, _password, _system, _systemidentity });
                if (responseObject is System.Xml.XmlNode)
                {
                    System.Xml.XmlNode responseNode = (System.Xml.XmlNode)responseObject;
                    xmlString = responseNode.OuterXml;
                }
                else if (responseObject is String)
                {
                    xmlString = (string)responseObject;
                }
                else
                {
                    Exception ex = new Exception("请求返回类型不在判断条件内");
                    LogHelper.LogError(ex);
                }
                LogHelper.LogInfo(xmlString);
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(xmlString);
                XmlNodeList rtnNodeList = xmlDoc.GetElementsByTagName("rtn");
                XmlNodeList msgNodeList = xmlDoc.GetElementsByTagName("msg");
                XmlNodeList userInformationNodeList = xmlDoc.GetElementsByTagName("userInformation");
                string rtnContent = String.Empty;
                string msgContent = String.Empty;
                string userInformation_ID = String.Empty;
                string userInformation_AccountName = String.Empty;

                if (rtnNodeList.Count > 0)
                {
                    rtnContent = rtnNodeList[0]?.InnerText;
                    msgContent = msgNodeList[0]?.InnerText;
                    userInformation_ID = userInformationNodeList[0]?.Attributes.GetNamedItem("ID").InnerText;
                    userInformation_AccountName = userInformationNodeList[0]?.Attributes.GetNamedItem("AccountName").InnerText;

                    LogHelper.LogInfo(String.Format("rtn:{0}, msg:{1}, ID:{2}, AccountName:{3}"
                        , rtnContent
                        , msgContent
                        , userInformation_ID
                        , userInformation_AccountName));
                    Console.WriteLine("调用成功");
                }
                else
                {
                    LogHelper.LogInfo("RtnNodeList'length = 0");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("调用失败");
                LogHelper.LogError(ex);
            }

            Console.ReadLine();
        }
    }
}
